<template>
  <ych-modal
    :fullscreen="false"
    :title="title"
    :visible="visible"
    :destroyOnClose="true"
    @cancel="handleCancel">

    <a-tabs size="small" :activeKey="activeKey" @change="handleChange">
      <a-tab-pane tab="当前应用" :key="comp[0]" :forceRender="true">
        <act-app-form-list :ref="comp[0]" :radio="radio" :groupId="groupId"
                           @selected="handleOk" style="margin-bottom: 8px;"></act-app-form-list>
        <act-app-view-layout-list :ref="comp[3]" :radio="radio" :groupId="groupId"
                                  @selected="handleOk"></act-app-view-layout-list>
      </a-tab-pane>
      <a-tab-pane tab="公共应用" :key="comp[1]" :forceRender="true">
        <act-app-form-common-list :ref="comp[1]" :radio="radio" :groupId="groupId"
                                  @selected="handleOk" style="margin-bottom: 8px;"></act-app-form-common-list>
        <act-app-view-layout-common-list :ref="comp[4]" :radio="radio" :groupId="groupId"
                                         @selected="handleOk"></act-app-view-layout-common-list>
      </a-tab-pane>
      <a-tab-pane tab="注册表单" :key="comp[2]" :forceRender="true">
        <act-app-form-bpm-list :ref="comp[2]" :radio="radio" :groupId="groupId"
                               @selected="handleOk"></act-app-form-bpm-list>
      </a-tab-pane>
    </a-tabs>

    <template slot="footer">
      <a-button key="back" @click="handleCancel">{{ $t('m.common.button.close') }}</a-button>
      <a-button key="handleClear" @click="handleClear">{{ $t('m.common.button.clear') }}</a-button>
      <a-button key="submit" type="primary" @click="handleOk">{{ $t('m.common.button.ok') }}</a-button>
    </template>
  </ych-modal>
</template>

<script>

import ActAppFormList from '../list/ActAppFormList'
import ActAppFormBpmList from '../list/ActAppFormBpmList'
import { SelectDialogMixin } from './SelectDialogMixin'
import ActAppFormCommonList from '../list/ActAppFormCommonList'
import ActAppViewLayoutList from "../list/ActAppViewLayoutList";
import ActAppViewLayoutCommonList from "../list/ActAppViewLayoutCommonList";

export default {
  name: 'SelectFormTabDialog',
  mixins: [SelectDialogMixin],
  components: {
    ActAppFormList,
    ActAppFormBpmList,
    ActAppFormCommonList,
    ActAppViewLayoutList,
    ActAppViewLayoutCommonList
  },
  data() {
    return {
      title: this.$t('m.eform.formDesign.label.xuan-ze-biao-dan'),
      comp: ['comp', 'compCommon', 'bpmForm', 'compLayout', 'compCommonLayout']
    }
  },
  methods: {
    handleOk() {
      if (this.radio) {
        let selectedRowKeys = [];
        let selectionRows = [];
        let activeKey = 'eform';
        if (this.activeKey === this.comp[0]) {
          selectedRowKeys = this.$refs[this.comp[0]].selectedRowKeys;
          selectionRows = this.$refs[this.comp[0]].selectionRows;
          if (selectedRowKeys.length === 0) {
            selectedRowKeys = this.$refs[this.comp[3]].selectedRowKeys;
            selectionRows = this.$refs[this.comp[3]].selectionRows;
            activeKey = 'layout'
          }
        } else if (this.activeKey === this.comp[1]) {
          selectedRowKeys = this.$refs[this.comp[1]].selectedRowKeys;
          selectionRows = this.$refs[this.comp[1]].selectionRows;
          if (selectedRowKeys.length === 0) {
            selectedRowKeys = this.$refs[this.comp[4]].selectedRowKeys;
            selectionRows = this.$refs[this.comp[4]].selectionRows;
            activeKey = 'layout'
          }
        } else if (this.activeKey === this.comp[2]) {
          selectedRowKeys = this.$refs[this.comp[2]].selectedRowKeys;
          selectionRows = this.$refs[this.comp[2]].selectionRows;
          activeKey = 'bpmForm'
        }
        if (selectedRowKeys.length === 0) {
          this.messageUtil.warning(this.$t('m.eform.formDesign.label.qing-zai-dang-qian-ye-xuan-ze-shu-ju'))
          return
        }
        if (this.activeKey === this.comp[2]) {
          if (!selectionRows[0].pcUrl) {
            this.messageUtil.warning(this.$t('m.eform.formDesign.label.shu-ju-wu-xiao-pcurl-wei-pei-zhi'))
            return
          }
        }
        this.$emit('selectFinished', selectedRowKeys, selectionRows, activeKey)
        this.handleCancel()
      } else {
        this.messageUtil.warning(this.$t('m.eform.formDesign.label.zan-shi-bu-zhi-chi-duo-xuan'))
      }
    }
  }
}
</script>
